#include<stdio.h>
#include<graphics.h>
#define WIDTH 591
#define HEIGH 724
#define bull_num = 15;

struct player {
	int x;
	int y;
	bool live;
}player , bull[15];
void game_init()
{
	player.x = WIDTH / 2 - 60;
	player.y = HEIGH - 120;
	player.live = true;

	for (int i = 0; i < 15; i++)
	{
		bull[i].x = 0;
		bull[i].y = 0;
		bull[i].live = false;
	}
}
void bull_create() {
	for (int i = 0; i < 15; i++) {
		bull[i].x = player.x + 50;
		bull[i].y = player.y;
		bull[i].live = true;
	}
}
void bull_move()
{
	for (int i = 0; i < 15; i++) {
		if (bull[i].live) {
			bull[i].y -= 2;
			
			if (bull[i].y < 0)
				bull[i].live = false;
		}

	}

}
void play_move(int speed)
{

	if (GetAsyncKeyState(VK_SPACE))
		bull_create();
	if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState('W')) {
	if(player.y > 0)
		player.y -= speed;
	}
	if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('S')) {
	if(player.y < HEIGH - 100)
		player.y += speed;
	}
	if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A')) {
	if(player.x > -50)
		player.x -= speed;
	}
	if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D')) {
	if(player.x < WIDTH-50)
		player.x += speed;
	}
}
void game_draw()
{
	IMAGE bullet[2];
	IMAGE bk;
	IMAGE planemodel[2];
	loadimage(&bk, "./background.png",WIDTH,HEIGH);
	loadimage(&planemodel[0], "./planeNormal_1.jpg");
	loadimage(&planemodel[1], "./planeNormal_2.jpg");
	loadimage(&bullet[0], "./bullet1.jpg");
	loadimage(&bullet[1], "./bullet2.jpg");
	putimage(0, 0, &bk);
	
		
		putimage(player.x, player.y, &planemodel[0], NOTSRCERASE);
		putimage(player.x, player.y, &planemodel[1], SRCINVERT);
		
		bull_move();
		for (int i = 0; i < 15; i++)
		{
			if (bull[i].live) {
				putimage(bull[i].x, bull[i].y, &bullet[0], NOTSRCERASE);
				putimage(bull[i].x, bull[i].y, &bullet[1], SRCINVERT);
			}
		}

}

int main()
{
	initgraph(WIDTH, HEIGH);
	game_init();
	BeginBatchDraw();
	while (1) {
		game_draw();
		FlushBatchDraw();
		play_move(3);
		bull_move();
	}
	EndBatchDraw();
	return 0;
}